package demo.db.mybatis.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class MainGenerator {

    public static void main(String[] args) throws IOException {
//        System.setProperty("javax.xml.accessExternalDTD", "all");

        String path = "db/mybatis/generator/generatorConfig.xml";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try (InputStream is = classLoader.getResourceAsStream(path)) {
            if (is == null) {
                System.err.println("获取资源失败：" + path);
                return;
            }
            // 解析
            List<String> warnings = new ArrayList<String>();
            ConfigurationParser cp = new ConfigurationParser(warnings);

            Configuration configuration = cp.parseConfiguration(is);
            // 是否覆盖
            DefaultShellCallback dsc = new DefaultShellCallback(true);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, dsc, warnings);
            myBatisGenerator.generate(null, new HashSet<String>(Arrays.asList("ops_alert")));

            if (warnings.isEmpty()) {
                System.out.println("OK");
            } else {
                warnings.forEach(System.err::println);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
